{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 地理编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests,json\n",
    "import pandas as pd\n",
    "chen_key=\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 准备数据\n",
    "用户定位=\"广东省广州市中山大学南方学院东学楼18号\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url=\"https://restapi.amap.com/v3/geocode/geo?parameters\"\n",
    "params={\n",
    "    \"key\":chen_key,\n",
    "    \"address\":用户定位\n",
    "}\n",
    "response=requests.get(url,params=params)\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': '1',\n",
       " 'info': 'OK',\n",
       " 'infocode': '10000',\n",
       " 'count': '1',\n",
       " 'geocodes': [{'formatted_address': '广东省广州市从化区中山大学南方学院东学楼|18',\n",
       "   'country': '中国',\n",
       "   'province': '广东省',\n",
       "   'citycode': '020',\n",
       "   'city': '广州市',\n",
       "   'district': '从化区',\n",
       "   'township': [],\n",
       "   'neighborhood': {'name': [], 'type': []},\n",
       "   'building': {'name': [], 'type': []},\n",
       "   'adcode': '440117',\n",
       "   'street': [],\n",
       "   'number': [],\n",
       "   'location': '113.679428,23.633869',\n",
       "   'level': '门牌号'}]}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 路径规划"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#准备数据\n",
    "出发点=\"广东省广州市中山大学南方学院东学楼18号\"\n",
    "目的地=\"广东省广州市中山大学南方学院教学楼4号\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def geocode(key,address,city=None,batch=None)->str:\n",
    "    \"\"\"获取高德API的地理编码\n",
    "    注释：key是指高德API的秘钥，请先去注册\n",
    "          address是指结构化地址，具体请查看：https://lbs.amap.com/api/webservice/guide/api/georegeo   \n",
    "    \"\"\"\n",
    "    url = \"https://restapi.amap.com/v3/geocode/geo?parameters\"\n",
    "    params = {\n",
    "        \"key\":key,\n",
    "        \"address\":address,\n",
    "        \"city\":city,\n",
    "        \"batch\":batch\n",
    "    }\n",
    "    response = requests.get(url,params=params)\n",
    "    results = response.json()['geocodes'][0]['location']\n",
    "    # 返回值\n",
    "    return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'113.679428,23.633869'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "origin = geocode(chen_key,出发点)\n",
    "origin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'113.677460,23.630490'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "destination = geocode(chen_key,目的地)\n",
    "destination"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = \"https://restapi.amap.com/v3/direction/driving?parameters\"\n",
    "params = {\n",
    "    \"key\":chen_key,\n",
    "    \"origin\":origin,\n",
    "    \"destination\":destination\n",
    "}\n",
    "r = requests.get(url,params)\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'distance': '766',\n",
       " 'duration': '175',\n",
       " 'strategy': '速度最快',\n",
       " 'tolls': '0',\n",
       " 'toll_distance': '0',\n",
       " 'steps': [{'instruction': '向东北行驶17米右转',\n",
       "   'orientation': '东北',\n",
       "   'distance': '17',\n",
       "   'tolls': '0',\n",
       "   'toll_distance': '0',\n",
       "   'toll_road': [],\n",
       "   'duration': '10',\n",
       "   'polyline': '113.679418,23.633876;113.679462,23.633911;113.679549,23.633967',\n",
       "   'action': '右转',\n",
       "   'assistant_action': [],\n",
       "   'tmcs': [{'lcode': [],\n",
       "     'distance': '17',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.679418,23.633876;113.679462,23.633911;113.679549,23.633967'}],\n",
       "   'cities': [{'name': '广州市',\n",
       "     'citycode': '020',\n",
       "     'adcode': '440100',\n",
       "     'districts': [{'name': '从化区', 'adcode': '440117'}]}]},\n",
       "  {'instruction': '向南行驶32米向右前方行驶',\n",
       "   'orientation': '南',\n",
       "   'distance': '32',\n",
       "   'tolls': '0',\n",
       "   'toll_distance': '0',\n",
       "   'toll_road': [],\n",
       "   'duration': '15',\n",
       "   'polyline': '113.679549,23.633967;113.679648,23.633689',\n",
       "   'action': '向右前方行驶',\n",
       "   'assistant_action': [],\n",
       "   'tmcs': [{'lcode': [],\n",
       "     'distance': '32',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.679549,23.633967;113.679648,23.633689'}],\n",
       "   'cities': [{'name': '广州市',\n",
       "     'citycode': '020',\n",
       "     'adcode': '440100',\n",
       "     'districts': [{'name': '从化区', 'adcode': '440117'}]}]},\n",
       "  {'instruction': '向南行驶131米左转',\n",
       "   'orientation': '南',\n",
       "   'distance': '131',\n",
       "   'tolls': '0',\n",
       "   'toll_distance': '0',\n",
       "   'toll_road': [],\n",
       "   'duration': '28',\n",
       "   'polyline': '113.679648,23.633689;113.679627,23.63365;113.679596,23.633251;113.679592,23.633173;113.679596,23.632856;113.679596,23.632491',\n",
       "   'action': '左转',\n",
       "   'assistant_action': [],\n",
       "   'tmcs': [{'lcode': [],\n",
       "     'distance': '48',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.679648,23.633689;113.679627,23.63365;113.679596,23.633251'},\n",
       "    {'lcode': [],\n",
       "     'distance': '9',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.679596,23.633251;113.679592,23.633173'},\n",
       "    {'lcode': [],\n",
       "     'distance': '34',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.679592,23.633173;113.679596,23.632856'},\n",
       "    {'lcode': [],\n",
       "     'distance': '40',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.679596,23.632856;113.679596,23.632491'}],\n",
       "   'cities': [{'name': '广州市',\n",
       "     'citycode': '020',\n",
       "     'adcode': '440100',\n",
       "     'districts': [{'name': '从化区', 'adcode': '440117'}]}]},\n",
       "  {'instruction': '向东行驶100米右转',\n",
       "   'orientation': '东',\n",
       "   'distance': '100',\n",
       "   'tolls': '0',\n",
       "   'toll_distance': '0',\n",
       "   'toll_road': [],\n",
       "   'duration': '24',\n",
       "   'polyline': '113.679596,23.632491;113.680582,23.632522',\n",
       "   'action': '右转',\n",
       "   'assistant_action': [],\n",
       "   'tmcs': [{'lcode': [],\n",
       "     'distance': '100',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.679596,23.632491;113.680582,23.632522'}],\n",
       "   'cities': [{'name': '广州市',\n",
       "     'citycode': '020',\n",
       "     'adcode': '440100',\n",
       "     'districts': [{'name': '从化区', 'adcode': '440117'}]}]},\n",
       "  {'instruction': '向南行驶199米向右前方行驶',\n",
       "   'orientation': '南',\n",
       "   'distance': '199',\n",
       "   'tolls': '0',\n",
       "   'toll_distance': '0',\n",
       "   'toll_road': [],\n",
       "   'duration': '43',\n",
       "   'polyline': '113.680582,23.632522;113.680642,23.632135;113.680668,23.631736;113.680642,23.631589;113.680629,23.631536;113.68059,23.631432;113.680499,23.631237;113.680399,23.631089;113.680204,23.630838',\n",
       "   'action': '向右前方行驶',\n",
       "   'assistant_action': [],\n",
       "   'tmcs': [{'lcode': [],\n",
       "     'distance': '122',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.680582,23.632522;113.680642,23.632135;113.680668,23.631736;113.680642,23.631589;113.680629,23.631536;113.68059,23.631432'},\n",
       "    {'lcode': [],\n",
       "     'distance': '77',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.68059,23.631432;113.680499,23.631237;113.680399,23.631089;113.680204,23.630838'}],\n",
       "   'cities': [{'name': '广州市',\n",
       "     'citycode': '020',\n",
       "     'adcode': '440100',\n",
       "     'districts': [{'name': '从化区', 'adcode': '440117'}]}]},\n",
       "  {'instruction': '向西行驶287米到达目的地',\n",
       "   'orientation': '西',\n",
       "   'distance': '287',\n",
       "   'tolls': '0',\n",
       "   'toll_distance': '0',\n",
       "   'toll_road': [],\n",
       "   'duration': '55',\n",
       "   'polyline': '113.680204,23.630838;113.68003,23.630768;113.67964,23.630634;113.679358,23.630516;113.679197,23.630469;113.678711,23.63036;113.678212,23.630352;113.677457,23.630347',\n",
       "   'action': [],\n",
       "   'assistant_action': '到达目的地',\n",
       "   'tmcs': [{'lcode': [],\n",
       "     'distance': '18',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.680204,23.630838;113.68003,23.630768'},\n",
       "    {'lcode': [],\n",
       "     'distance': '42',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.68003,23.630768;113.67964,23.630634'},\n",
       "    {'lcode': [],\n",
       "     'distance': '48',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.67964,23.630634;113.679358,23.630516;113.679197,23.630469'},\n",
       "    {'lcode': [],\n",
       "     'distance': '51',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.679197,23.630469;113.678711,23.63036'},\n",
       "    {'lcode': [],\n",
       "     'distance': '51',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.678711,23.63036;113.678212,23.630352'},\n",
       "    {'lcode': [],\n",
       "     'distance': '77',\n",
       "     'status': '未知',\n",
       "     'polyline': '113.678212,23.630352;113.677457,23.630347'}],\n",
       "   'cities': [{'name': '广州市',\n",
       "     'citycode': '020',\n",
       "     'adcode': '440100',\n",
       "     'districts': [{'name': '从化区', 'adcode': '440117'}]}]}],\n",
       " 'restriction': '0',\n",
       " 'traffic_lights': '0'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['route']['paths'][0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 静态地图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = \"https://restapi.amap.com/v3/staticmap?parameters\"\n",
    "params = {\n",
    "    \"key\":chen_key,\n",
    "    \"location\":origin,\n",
    "    \"zoom\":4\n",
    "}\n",
    "r = requests.get(url,params)\n",
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 轨迹纠编\n",
    "[官方demo](https://lbs.amap.com/api/amap-ui/demos/amap-ui-pathsimplifier/simple-demo)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
